﻿using Daemon.Service.Contract;
using Daemon.Model;
using Microsoft.AspNetCore.Http;
using System.Linq;
using System;
using Microsoft.AspNetCore.Mvc;
using Daemon.Repository.Contract;
using System.Collections.Generic;
using Daemon.Model.ViewModel;

namespace Daemon.Service
{
	public class CommonCityService : ICommonCityService
	{
		private readonly ICommonCityRepository _commonCityRepository;
		public CommonCityService(ICommonCityRepository commonCityRepository)
		{
			_commonCityRepository = commonCityRepository;
		}

		public List<CityData> GetCommonCity()
		{
			var commonCityList = _commonCityRepository.FindAll().ToList();
			return AppendCityData(commonCityList, 1, 0);
		}

		private List<CityData> AppendCityData(List<CommonCity> commonCityList, int level, int parentId)
		{
			var result = new List<CityData>();
			var provinceList = commonCityList.Where(r => r.Level == level && r.ParentId == parentId).ToList();
			provinceList.ForEach(item =>
			{
				var cityData = new CityData();
				cityData.value = item.Value;
				cityData.label = item.Label;
				cityData.children = AppendCityData(commonCityList, item.Level + 1, item.Value);
				result.Add(cityData);
			});

			return result;
		}
	}
}
